CurrentVersion = 1.0
ModdedName = "" //Change this when you modify the gamemode, please.

GM.Name 	= "Terminal Velocity (v"..CurrentVersion..ModdedName..")"
GM.Author 	= "Rambo_6"
GM.Email 	= "sechs.llsc.us@gmail.com"
GM.Website 	= "hai2u.com"

TEAM_RED = 1
TEAM_BLUE = 2

if (SERVER) then
	if GameInfo.MusicEnabled then
		resource.AddFile( "sound/music/win1.mp3" )
		resource.AddFile( "sound/music/win2.mp3" )
		resource.AddFile( "sound/music/win3.mp3" )
		resource.AddFile( "sound/music/lose1.mp3" )
		resource.AddFile( "sound/music/lose2.mp3" )
		resource.AddFile( "sound/music/lose3.mp3" )
	end
end

function GM:SetupTeams()
	team.SetUp(1, "Red Barons", Color(180, 20, 20, 255))  
	team.SetUp(2, "Blue Bombers", Color(20, 20, 180, 255)) 
	team.SetUp(3, "Spectators", Color(20, 200, 20, 255)) 
end

function GM:PickNextMap()
	local message = "Loading next map..."
	for k, p in pairs(player.GetAll()) do
		p:PrintMessage(HUD_PRINTTALK, message)
	end
	game.LoadNextMap() 
end

function GM:EndRound(losingteam)

	for k,v in pairs(ents.FindByClass("sent_beacon")) do
		v:Remove()
	end
	
	if losingteam == 2 then
		GameInfo.RedScore = GameInfo.RedScore + 1
	else
		GameInfo.BlueScore = GameInfo.BlueScore + 1
	end
	
	for k,v in pairs(player.GetAll()) do
		v:ExitVehicle()
		v:GodEnable()
		v:DestroyVehicle(nil)
		umsg.Start("UpdateRedScore",v)
		umsg.Short(GameInfo.RedScore)
		umsg.End()
		umsg.Start("UpdateBlueScore",v)
		umsg.Short(GameInfo.BlueScore)
		umsg.End()
	end
	
	if ((GameInfo.RedScore + 1) >= GameInfo.MaxScore) or ((GameInfo.BlueScore + 1) >= GameInfo.MaxScore) then
		for k, v in pairs(player.GetAll()) do
			v:ChatPrint("The next map will be "..game.GetMapNext().. ".")
		end
	end
	
	if losingteam == 2 then
		for k, v in pairs(team.GetPlayers(1)) do
			v:ChatPrint(ChooseString(RoundWin))
			if GameInfo.MusicEnabled then
				umsg.Start("PlaySong",v)
				umsg.Short(5)
				umsg.End()
			end
		end	
		for k, v in pairs(team.GetPlayers(2)) do
			v:ChatPrint(ChooseString(RoundLose))
			if GameInfo.MusicEnabled then
				umsg.Start("PlaySong",v)
				umsg.Short(6)
				umsg.End()
			end
		end	
	else
		for k, v in pairs(team.GetPlayers(2)) do
			v:ChatPrint(ChooseString(RoundWin))
			if GameInfo.MusicEnabled then
				umsg.Start("PlaySong",v)
				umsg.Short(5)
				umsg.End()
			end
		end	
		for k, v in pairs(team.GetPlayers(1)) do
			v:ChatPrint(ChooseString(RoundLose))
			if GameInfo.MusicEnabled then
				umsg.Start("PlaySong",v)
				umsg.Short(6)
				umsg.End()
			end
		end	
	end
	
	if GameInfo.RedScore >= GameInfo.MaxScore or GameInfo.BlueScore >= GameInfo.MaxScore then
		timer.Create("ScoreTimer", 0.1, 1, GAMEMODE.DisplayHighScores, GAMEMODE)
		timer.Create("RoundTimer", GameInfo.RoundDelay, 1, GAMEMODE.StartRound, GAMEMODE)
		return
	end
	
	timer.Create("RoundTimer", GameInfo.RoundDelay, 1, GAMEMODE.StartRound, GAMEMODE)
	
end

function GM:LoadNextMap()
	game.LoadNextMap()
end

function GM:StartRound()

	for k,v in pairs(player.GetAll()) do
		v:SetNetworkedInt("beaconhealth2",GameInfo.BeaconHealth)
		v:SetNetworkedInt("beaconhealth1",GameInfo.BeaconHealth)
	end
	
	for k,v in pairs(GameInfo.BeaconPositions) do
		local ent = ents.Create( "sent_beacon" )
		ent:SetPos( v )
		ent:SetTeam(GameInfo.BeaconTeams[k])
		ent:Spawn()
		ent:Activate()
	end
	
	if GameInfo.RedScore >= GameInfo.MaxScore or GameInfo.BlueScore >= GameInfo.MaxScore then
		timer.Create("MapTimer", GameInfo.EndGameDelay, 1, GAMEMODE.LoadNextMap, GAMEMODE)
		for k,v in pairs(player.GetAll()) do
			v:ChatPrint("Next map: "..game.GetMapNext())
			v:Spawn()
		end
		return
	end
	
	for k,v in pairs(player.GetAll()) do
		v:GodDisable()
		v:ChatPrint(ChooseString(RoundStart))
		v:Spawn()
	end
	
end

function GM:DisplayHighScores()
	for k,v in pairs(player.GetAll()) do
		umsg.Start("ShowScores", v)
		umsg.Short( CurTime() )
		umsg.End() 
	end
end

function GM:PlayerShouldTakeDamage( ply, attacker )
	
	if attacker:IsPlayer() then
		if ply:Team() == attacker:Team() and attacker != ply then
			return false
		else
			return true
		end
	end
	
	if !attacker:IsPlayer() then
		return true
	end
end

function GM:SetupMove( ply, mv )
	return false
end

function GM:Move( ply, mv )
	return false
end

function GM:PlayerTraceAttack( ply, dmginfo, dir, trace )

	if not dmginfo then return end
	
	local attacker = dmginfo:GetAttacker()
	local dmg = dmginfo:GetDamage()
	
	if !attacker:IsValid() or attacker == ply then
		return
	end
	
	if ply.IsInVehicle then return end
	
	if attacker:Team() != ply:Team() and ply:Health() > 50 then
		ply.DeathType = "normal"
		local num = math.random(1,15)
		if num == 6 and ply.Pain then
			ply:EmitSound(Sound(ChooseRandom(ply.Pain)),100,math.random(95,105))
		end
	end
	
	util.Decal("Blood", trace.HitPos + trace.HitNormal, trace.HitPos - trace.HitNormal)
	
	local effectdata = EffectData()
	effectdata:SetOrigin(trace.HitPos)
	effectdata:SetNormal(trace.HitNormal)
	util.Effect("bodyshot", effectdata)

	return self.BaseClass:PlayerTraceAttack(ply, dmginfo, dir, trace)
end
